package com.qfedu.service.impl;

import com.qfedu.dao.AccountDao;
import com.qfedu.dao.CreditCardDao;
import com.qfedu.dao.impl.AccountDaoImpl;
import com.qfedu.dao.impl.CreditCardDaoImpl;
import com.qfedu.pojo.Account;
import com.qfedu.pojo.Cards;
import com.qfedu.pojo.CreditCard;
import com.qfedu.service.AccountService;
import com.qfedu.service.CreditCardService;
import com.qfedu.utils.DruidUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CreditCardServiceImpl implements CreditCardService {
    AccountDao accountDao = new AccountDaoImpl();
    CreditCardDao creditCardDao = new CreditCardDaoImpl();
    @Override
    public CreditCard findCreditCard(String phone) {
        return creditCardDao.findCreditCard(phone);
    }

    @Override
    public int addCreditCard(CreditCard creditCard) {
        return creditCardDao.addCreditCard(creditCard);
    }

    @Override
    public CreditCard findCardsnumber(String cardnumber) {
        return creditCardDao.findCardsnumber(cardnumber);
    }

    @Override
    public void transfer(Account source, CreditCard target, int money) {
        Connection connection = DruidUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            accountDao.outMoney(source,money,connection);
            //int i = 1/0;
            creditCardDao.inMoney(target,money,connection);
            connection.commit();

        } catch (SQLException throwables) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            throwables.printStackTrace();
        }
    }

    @Override
    public List<CreditCard> findIdcodeCredit(String idcode) {
        return creditCardDao.findIdcodeCredit(idcode);
    }
}
